#include "image.h"


float Image::getWidth()
{
	return imageDesc.Width;
}

float Image::getHeight()
{
	return imageDesc.Height;
}

float Image::HalfHeight()
{
	return getHeight() * 0.5f;
}

float Image::HalfWidth()
{
	return getWidth() * 0.5f;
}

void Image::setColorMap(ID3D11ShaderResourceView* colorMap, ID3D11Device* device)
{
	Image::colorMap = colorMap;

	ID3D11Resource* colorTex;
    colorMap->GetResource( &colorTex );

    ( ( ID3D11Texture2D* )colorTex )->GetDesc( &imageDesc );
    colorTex->Release( );

	DirectXInterface::VertexPos vertices[] =
    {
		{ XMFLOAT3(  HalfWidth(),  HalfHeight(), 1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
        { XMFLOAT3(  HalfWidth(), -HalfHeight(), 1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
        { XMFLOAT3( -HalfWidth(), -HalfHeight(), 1.0f ), XMFLOAT2( 0.0f, 1.0f ) },

        { XMFLOAT3( -HalfWidth(), -HalfHeight(), 1.0f ), XMFLOAT2( 0.0f, 1.0f ) },
        { XMFLOAT3( -HalfWidth(),  HalfHeight(), 1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
        { XMFLOAT3(  HalfWidth(),  HalfHeight(), 1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
    };

	Image::vertices = vertices;

	D3D11_BUFFER_DESC vertexDesc;
    ZeroMemory( &vertexDesc, sizeof( vertexDesc ) );
    vertexDesc.Usage = D3D11_USAGE_DEFAULT;
    vertexDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    vertexDesc.ByteWidth = sizeof( DirectXInterface::VertexPos ) * 6;

    D3D11_SUBRESOURCE_DATA resourceData;
    ZeroMemory( &resourceData, sizeof( resourceData ) );
    resourceData.pSysMem = vertices;

	HRESULT d3dResult;

    d3dResult = device->CreateBuffer( &vertexDesc, &resourceData, &vertexBuffer );

    if( FAILED( d3dResult ) )
    {
        DXTRACE_MSG( "Failed to create vertex buffer!" );
        return;
    }
}

void Image::setColorSampler(ID3D11SamplerState* colorMapSampler)
{
	Image::colorMapSampler = colorMapSampler;
}

void Image::setBlendState(ID3D11BlendState* blendState)
{
	Image::alphaBlendState = blendState;
}

void Image::setPixelShaderInfo(ID3D11PixelShader* pixelShader)
{
	Image::pixelShaderInfo = pixelShader;
}

void Image::setVertexShaderInfo(ID3D11VertexShader* vertexShader)
{
	Image::vertexShaderInfo = vertexShader;
}


void Image::setInputLayout(ID3D11InputLayout* il)
{
	Image::inputLayout = il;
}